package Gui;


import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import main.Main;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import Objects.Body;
import Objects.BodyCircle;
import Objects.BodyRectangle;


public class SetobjectsState extends BasicGameState{
	
	@SuppressWarnings("unchecked")
	public HashMap []hM;
	public HashMap<Integer, Body> bodies;
    public HashMap<Body, BodyRectangle> btr;
    public HashMap<Body, BodyCircle> btc;
	int stateID = -1;
	float x,y;
	Toolbar tb;
	Image background;
    Boolean exit = false;
	JFrame frame;
	JPanel panel;
	boolean aflag=true;
	
	BodyCircle circle;
	BodyRectangle rectangle;
	 
    @SuppressWarnings("unchecked")
	public SetobjectsState(int stateID, HashMap[] hM ) 
    {
       this.stateID = stateID;
       this.hM=hM;
       bodies=hM[0];
       btr=hM[1];
       btc=hM[2];
    }
 
    @Override
    public int getID() {
        return 1;
    }
    
    
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    	background = new Image("/Images/click.png");	
    	gc.setTargetFrameRate(1);
    	frame=new JFrame("Toolbar");
//    	panel=new JPanel();
//		frame.add(panel);
    	
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    	background.draw(0, 0);
    	//frame.add(tb);
    	for(int i=0; i<bodies.size(); i++){
    		g.fill(bodies.get(i).shape);
    	}
    	//narysowac obiekty dodane przez toolbar
    }
 
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    	Input input = gc.getInput();
		
    	if(tb!=null){
    		if(tb.exit){
				//System.out.println("IN");
				if(bodies.size()>=2){
					sbg.enterState(Main.GAMEPLAYSTATE);
    			}
				else
				{
					JOptionPane.showMessageDialog(null, "There is not enough bodies to hava a chance of collison.", "Create more bodies", JOptionPane.WARNING_MESSAGE);
					tb.setVisible(false);
					tb.exit=false;
				}
    		}
    	}
    	
    	if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
			if(aflag){
				tb = new Toolbar(x,y,exit,hM, frame);
				tb.init();
				frame.add(tb.tb);
				aflag=false;
		}
    	x=input.getMouseX();
    	y=input.getMouseY();
		tb.set(x,y);
		//tb.setLocation((int)x,(int)y);
		tb.setVisible(true);
		//System.out.println("x=" + x + "y=" +y);
		
		frame.setLocation((int)x,(int)y); //TODO it's relative to the place of the slick window
    	frame.setResizable(false);
		frame.pack();
    	frame.setVisible(true);
    	
//    	circle=new BodyCircle(1, 200, 100, 27 );
//    	circle.setVx(15);
//		circle.setVy(-2);
//		bodies.put(bodies.size(),circle);
//		btc.put(bodies.get(bodies.size()-1),circle);
//    	
//		rectangle=new BodyRectangle(1, 700, 200, 40, 40);
//		rectangle.setVx(-7);
//		rectangle.setVy(-5);
//		rectangle.setAngularVelocity(0);
//		bodies.put(bodies.size(),rectangle);
//		btr.put(bodies.get(bodies.size()-1),rectangle);
		
		sbg.enterState(Main.GAMEPLAYSTATE);
    }
    	panel.remove(tb);
    	
		
    }

}
